package Study.demo12;

import java.sql.*;

public class jdbcPrepares2 {

    public static Connection connect(String URL, String user, String pass) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn=DriverManager.getConnection(URL, user, pass);
        return conn;

    }
    public static void select(String sql,Connection conn,String[] column) throws SQLException {
         PreparedStatement prepare= conn.prepareStatement(sql);
         ResultSet rs=prepare.executeQuery();
         while(rs.next()){
             for(int i=0;i<column.length;i++){
                 System.out.print(rs.getString(column[i])+" ");
             }
             System.out.println();
         }
         prepare.close();
    }
    public static void update(String sql,Connection conn,String[] column) throws SQLException {
                PreparedStatement prepral=conn.prepareStatement(sql);
                for(int i=0;i<column.length;i++){
                    prepral.setString(i,column[i]);
                }
                prepral.executeUpdate();
                prepral.close();
    }
    public  static ResultSet query(String sql, Connection conn, Object...values) throws SQLException {
         PreparedStatement pstmt=conn.prepareStatement(sql);
        for(int i=0;i<values.length;i++){
            pstmt.setObject(i+1,values[i]);
        }
        ResultSet rs=pstmt.executeQuery();
        return rs;

    }
    public static int dml(String sql, Connection conn, Object...values) throws SQLException {
        PreparedStatement pstmt=conn.prepareStatement(sql);
        for(int i=0;i<values.length;i++){
            pstmt.setObject(i+1,values[i]);
        }
        int isright=pstmt.executeUpdate();
        return isright;
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        String sql="select * from Student";
        Connection conn=connect("jdbc:mysql://localhost:3306/aaa","root","");
        String[] column={"id","name","password"};
        select(sql,conn,column);
        ResultSet re=query(sql,conn);
        while(re.next()){
            System.out.print(re.getString("id")+"  ");
            System.out.print(re.getString("name")+"  ");
            System.out.print(re.getString("password")+"  ");
            System.out.println();
       }

       /*String sql1="delete from Student where id=?";
        int result=dml(sql1,conn,"12");
        System.out.println(result);*/
        //Object values的作用是可以自由决定值的个数
        String sql2="select * from Student where id=? or name=?";
        ResultSet res=query(sql2,conn,"15","zadf");
        while(res.next()){
            System.out.print(res.getString("id")+"  ");
            System.out.print(res.getString("name")+"  ");
            System.out.print(res.getString("password")+"  ");
            System.out.println();
        }
        String sql3="insert into Student (id,name,password) values(?,?,?)";
        int result=dml(sql3,conn,545,"李四","4512653");
        System.out.println(result);


    }
}
